﻿    using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface.EliminationModel;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl.EliminationModelRepository
{
    public class EliminationModelRepository:IEliminationModelRepository
    {
        private readonly AgricultureDbContext _context;
        public EliminationModelRepository(AgricultureDbContext _context)
        {
            this._context = _context;
        }
        /// <summary>
        /// 添加淘汰信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> Add(List<Eliminationmodel> entity)
        {
            try
            {
                await _context.AddRangeAsync(entity);

                return await _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 逻辑删除功能
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Delete(int id)
        {
            try
            {
                var list = _context.Eliminationmodels.FirstOrDefault(x => x.EliminationId == id);
                if (list != null)
                {
                    list.LogicalDeletion = true;
                    _context.Update(list);
                }
                return _context.SaveChangesAsync();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 批量逻辑删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<int> DeleteRange(IEnumerable<int> ids)
        {
            foreach (var item in ids)
            {
                var list = _context.Eliminationmodels.FirstOrDefault(x => x.EliminationId == item);
                if (list != null)
                {
                    list.LogicalDeletion = true;
                    _context.Update(list);
                }
            }
            return await _context.SaveChangesAsync();
        }
        /// <summary>
        /// 反填功能
        /// </summary>
        /// <param name="id">通过id查找</param>
        /// <returns>返回列表</returns>
        public async Task<Eliminationmodel> Find(int id)
        {
            try
            {
                var list =  _context.Eliminationmodels.FirstOrDefault(x => x.EliminationId == id);
                return list;
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 显示，查询，分页
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Eliminationmodel>> GetShow()
        {
            try
            {
                return _context.Eliminationmodels.Where(x => x.LogicalDeletion == false);
            }
            catch (Exception)
            {

                throw;
            }
        }


        /// <summary>
        /// 修改功能
        /// </summary>
        /// <param name="entity">修改实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateAsync(Eliminationmodel entity)
        {
            _context.Entry(entity).State = EntityState.Modified;

            return await _context.SaveChangesAsync();
        }

        
    }
}
